import logging
import time

from appium.webdriver.common.appiumby import AppiumBy
from cash_pos.constants.common_sequence import SWITCH_ENV

from cash_pos.function.common_function import CommonFunction
from cash_pos.page.order_page.order_list_page import OrderListPage
from cash_pos.page.order_page.order_page import OrderPage
from cash_pos.page.security_page.daily_settlement_page import DailySettlementPage


class CommonPage(object):

    def __init__(self, client):
        self.client = client
        self.driver = client.driver

    def screen_shoot(self, filename, path):
        """
        屏幕截图
        Params:
        -filename:文件名称
        -path:路径
        """
        CommonFunction(self.driver).screen_shoot(filename=filename, path=path)
        return self

    def switch_menu(self, name):
        """
        切换侧边栏
        """
        menu_items = self.driver.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/cl_main_view')
        if name == "table":
            menu_items[1].click()
        elif name == "book":
            menu_items[0].click()
        elif name == "order":
            menu_items[2].click()
        elif name == "vip":
            menu_items[3].click()
        elif name == "handover":
            menu_items[4].click()
        elif name == "setting":
            menu_items[5].click()

    def find_element(self, value1, value2):
        """
        找某个元素
        Params:
        -value1:查找方式
        -value2:值
        """
        try:
            self.driver.find_element(value1, value2).click()
        except Exception as e:
            logging.error("标签元素未找到！")

    def time_wait(self, time: int):
        """
        延迟等待
        """
        self.driver.implicitly_wait(time)
        return self


    def time_sleep(self,seconds:int):
        """
        time模块等待
        Params:
        -seconds:时间
        """
        time.sleep(seconds)
        return self
